package com.zxy.zxyrpc.serializer;

import com.zxy.zxyrpc.model.RpcRequest;
import com.zxy.zxyrpc.model.RpcResponse;
import org.nustaq.serialization.FSTConfiguration;

import java.io.IOException;

/**
 * FST 序列化器（java自带，通用性不好，但性能跟k差不多）
 *
 * @author zxy
 * @since 2025/5/11
 **/
public class FstSerializer implements Serializer {
    // fst线程安全，但未避免频繁调用成为性能瓶颈。故采用 Configuration()
    private static final ThreadLocal<FSTConfiguration> FST = ThreadLocal.withInitial(() -> {
        FSTConfiguration fst = FSTConfiguration.createDefaultConfiguration();
        return fst;
    });

    @Override
    public <T> byte[] serialize(T object) throws IOException {
        return FST.get().asByteArray(object);
    }

    @Override
    public <T> T deserialize(byte[] bytes, Class<T> type) throws IOException {
        Object obj = FST.get().asObject(bytes);
        return (T) obj;
    }
}
